inline unsigned int VSSpatial::AddMaterialInstance(VSMaterialR * pMaterial)
{
	if(!pMaterial)
		return 0;
	VSMaterialInstance * pMaterialInstance = VS_NEW VSMaterialInstance(pMaterial);
	m_pMaterialInstance.AddElement(pMaterialInstance);

	return m_pMaterialInstance.GetNum();
}
inline unsigned int VSSpatial::AddMaterialInstance(VSMaterialInstance * pMaterial)
{
	if(!pMaterial || !pMaterial->GetMaterial())
		return 0;
	VSMaterialInstance * pMaterialInstance = VS_NEW VSMaterialInstance(pMaterial->GetMaterialR());
	m_pMaterialInstance.AddElement(pMaterialInstance);

	return m_pMaterialInstance.GetNum();
}
inline  bool VSSpatial::SetMaterialInstance(VSMaterialInstance * pMaterial,unsigned int uiIndex)
{
	if (uiIndex >= m_pMaterialInstance.GetNum() || !pMaterial)
	{
		return false;
	}
	if (m_pMaterialInstance[uiIndex])
	{
		m_pMaterialInstance[uiIndex]->Clone(pMaterial,0);
	}
	else
	{
		VSMaterialInstance * pMaterialInstance = VS_NEW VSMaterialInstance(pMaterial->GetMaterialR());
		m_pMaterialInstance[uiIndex] = pMaterialInstance;
	}
	return true;
}
inline  bool VSSpatial::SetUseMaterialInstance(unsigned int uiIndex)
{
	if (uiIndex >= m_pMaterialInstance.GetNum())
	{
		return false;
	}
	m_uiCurUseMaterial = uiIndex;
	return true;
}
inline void VSSpatial::ClearAllMaterialInstance()
{
	m_pMaterialInstance.Clear();
}
inline void VSSpatial::DeleteMaterialInstance(unsigned int i)
{
	if(i >= m_pMaterialInstance.GetNum())
		return ;
	m_pMaterialInstance[i] = NULL;
}
inline unsigned int VSSpatial::DeleteMaterialInstance(VSMaterialInstance * pMaterial)
{
	if(!pMaterial)
		return VSMAX_INTEGER;
	for (unsigned int i= 0 ; i < m_pMaterialInstance.GetNum() ; i++)
	{
		if(pMaterial == m_pMaterialInstance[i])
		{
			m_pMaterialInstance[i] = NULL;
			return i;
		}
	}
	return VSMAX_INTEGER;
}
inline VSMaterialInstance * VSSpatial::GetUseMaterialInstance()const
{
	VSMaterialInstance * pMaterialInstance = NULL;
	if (m_uiCurUseMaterial < m_pMaterialInstance.GetNum())
	{
		pMaterialInstance = m_pMaterialInstance[m_uiCurUseMaterial];
	}

	if(pMaterialInstance)
	{
		return pMaterialInstance;
	}
	else
	{
		VSSpatial * pParent = DynamicCast<VSSpatial>(m_pParent);
		if (m_pParent)
		{
			return pParent->GetUseMaterialInstance();
		}
		else
			return NULL;

	}
	
}

inline VSMaterialInstance * VSSpatial::GetMaterialInstance(unsigned int i)const
{
	if(i >= m_pMaterialInstance.GetNum())
		return NULL;
	return m_pMaterialInstance[i];
}

inline unsigned int VSSpatial::AddCamera(VSCamera * pCamera)
{
	if(pCamera)
	{
		for (unsigned int i = 0 ; i < m_pCamera.GetNum() ;i++)
		{
			if (m_pCamera[i] == pCamera)
			{
				return i;
			}
		}
		m_pCamera.AddElement(pCamera);
		pCamera->SetParent(this);
		return m_pCamera.GetNum() - 1;
	}
	return VSMAX_INTEGER;
}
inline VSCamera * VSSpatial::GetCamera(unsigned int i)const
{
	if(i >= m_pCamera.GetNum())
		return NULL;
	return m_pCamera[i];
}
inline void VSSpatial::ClearAllCamera()
{
	for(unsigned int i = 0 ; i < m_pCamera.GetNum() ; i++)
	{
		m_pCamera[i]->SetParent(NULL);
	}
	m_pCamera.Clear();
}
inline void VSSpatial::DeleteCamera(unsigned int i)
{	
	if(i >= m_pCamera.GetNum())
		return ;
	VSCamera * TempCamera = m_pCamera[i];
	TempCamera->SetParent(NULL);
	m_pCamera.Erase(i);
	
}
inline unsigned int VSSpatial::DeleteCamera(VSCamera * pCamera)
{
	if(!pCamera)
		return VSMAX_INTEGER;
	for (unsigned int i= 0 ; i < m_pCamera.GetNum() ; i++)
	{
		if(pCamera == m_pCamera[i])
		{
			pCamera->SetParent(NULL);
			m_pCamera.Erase(i);
			
			return i;
		}
	}
	return VSMAX_INTEGER;
}
inline unsigned int VSSpatial::AddLight(VSLight * pLight)
{
	if(pLight)
	{
		for (unsigned int i = 0 ; i < m_pLight.GetNum() ;i++)
		{
			if (m_pLight[i] == pLight)
			{
				return i;
			}
		}
		m_pLight.AddElement(pLight);
		pLight->SetParent(this);
		return m_pLight.GetNum() - 1;
	}
	return VSMAX_INTEGER;
}
inline VSLight * VSSpatial::GetLight(unsigned int i)const
{
	if(i >= m_pLight.GetNum())
		return NULL;
	return m_pLight[i];
}
inline void VSSpatial::ClearAllLight()
{
	for(unsigned int i = 0 ; i < m_pLight.GetNum() ; i++)
	{
		m_pLight[i]->SetParent(NULL);
	}
	m_pLight.Clear();
}
inline void VSSpatial::DeleteLight(unsigned int i)
{	
	if(i >= m_pLight.GetNum())
		return ;
	VSLight * pLight = m_pLight[i];
	pLight->SetParent(NULL);
	m_pLight.Erase(i);

}
inline unsigned int VSSpatial::DeleteLight(VSLight * pLight)
{
	for (unsigned int i= 0 ; i <m_pLight.GetNum() ; i++)
	{
		if(pLight == m_pLight[i])
		{
			pLight->SetParent(NULL);
			m_pLight.Erase(i);
			return i;
		}
	}
	return VSMAX_INTEGER;
}
inline const VSAABB3 & VSSpatial::GetWorldAABB()const
{
	return m_WorldBV;

}
bool VSSpatial::GetIsNeedDraw()const
{
	return m_bIsNeedDraw;
}
void VSSpatial::SetIsNeedDraw(bool IsNeedDraw)
{
	m_bIsNeedDraw = IsNeedDraw;
}